package com.cms.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; 
import com.cms.common.StringUtils;
import com.cms.dao.mapper.CustomerFieldMapper;
import com.cms.exception.SystemDBException;
import com.cms.model.TrCustomerField;
import com.cms.model.TrLotteryPlayInfo;
import com.cms.redis.RedisService;
import com.cms.service.CustomerFieldService;
import com.cms.service.PlaySettingService;

/***
 * 
 * @author huang
 *
 */
@Service
public class CustomerFieldServiceImpl implements CustomerFieldService {

	  @Autowired
	  private CustomerFieldMapper customerFieldMapper;
	   
	  
	  @Autowired
	  private PlaySettingService playService;
	
	  /**
	   * 
	   * @param customerFields
	   * @throws SystemDBException
	   */
	  public void addCustomerField(String fieldHandler,List<TrCustomerField>customerFields)throws Exception
	  {
		  this.deleteCustomerField(fieldHandler);
		  if(customerFields==null || customerFields.isEmpty())
		  {
			  return;
		  }
		  customerFieldMapper.addCustomerField(customerFields);
		  handlerRedisCache(fieldHandler);
	  }
	
	  /***
	   * 
	   * @param customerFields
	   * @throws SystemDBException
	   */
	  public void deleteCustomerField(String handler)throws Exception
	  {
		  customerFieldMapper.deleteCustomerField(handler);
	  } 
	  
	  /***
	   * 
	   * @param fieldHandler
	   */
	  private void handlerRedisCache(String fieldHandler)throws Exception
	  { 
		  String playCatalog = StringUtils.substringBefore(fieldHandler, ":");
		  if(StringUtils.equals(playCatalog, "LotteryContentBo"))
		  {
			  String playId = StringUtils.substringAfter(fieldHandler, ":");
			  TrLotteryPlayInfo playInfo = playService.getPlayInfo(StringUtils.str2Int(playId));
			  StringBuilder sb = new StringBuilder("playGroupBo:");
			  sb.append(playInfo.getLotteryGroupId());
			  RedisService.getRedisService().del(sb.toString());
		  }
	  }
	  
 
 
}
